package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.center.SliverOrderResult;
import com.zhongxi.guoyuan.url.Url;

import java.util.List;

/**
 * Created by Administrator on 2017/1/19.
 */

public class SliverOrderAdapter extends BaseAdapter {

    private Context mContext;
    private List<SliverOrderResult.SliverOrderData> list;

    public SliverOrderAdapter(Context context, List<SliverOrderResult.SliverOrderData> coinData) {
        this.mContext = context;
        this.list = coinData;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_sliver_order, parent, false);
            holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
            holder.tvOrderId = (TextView) convertView.findViewById(R.id.tvOrderId);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvCoin = (TextView) convertView.findViewById(R.id.tvCoin);
            holder.ivDel = (ImageView) convertView.findViewById(R.id.ivDel);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        try{
            if(null!=list.get(position)&&!"".equals(list.get(position))){
                final SliverOrderResult.SliverOrderData data = list.get(position);
                if(null!=data.getShopthumbnail()&&!"".equals(data.getShopthumbnail())){
                    Glide.with(mContext).load(Url.IMG_URL + data.getShopthumbnail()).placeholder(R.mipmap.iv_eg).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(holder.ivImg);
                }
                if(null!=data.getId()&&!"".equals(data.getId())){
                    holder.tvOrderId.setText("订单号码："+data.getId());
                }else{
                    holder.tvOrderId.setText("订单号码：");
                }
                if(null!=data.getCtime()&&!"".equals(data.getCtime())){
                    holder.tvTime.setText("消费时间："+data.getCtime());
                }else{
                    holder.tvTime.setText("消费时间：");
                }
                if(null!=data.getShopname()&&!"".equals(data.getShopname())){
                    holder.tvName.setText("店铺名称："+data.getShopname());
                }else{
                    holder.tvName.setText("店铺名称：");
                }
                if(null!=data.getPrice()&&!"".equals(data.getPrice())){
                    holder.tvCoin.setText(data.getPrice()+"银币");
                }else{
                    holder.tvCoin.setText("*银币");
                }
                if(data.isSelect()){
                    holder.ivDel.setImageResource(R.mipmap.iv_select);
                }else{
                    holder.ivDel.setImageResource(R.mipmap.iv_noselect);
                }
                //删除选中
                holder.ivDel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(data.isSelect()){
                            data.setSelect(false);
                        }else{
                            data.setSelect(true);
                        }
                        notifyDataSetChanged();
                    }
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    public String getOrderids() {
        String str = "";
        for(int i=0;i<list.size();i++){
            if(list.get(i).isSelect())
                str += list.get(i).getId()+",";
        }
        return str.substring(0, str.length()-1);
    }

    class ViewHolder{
        private ImageView ivImg;
        private TextView tvOrderId;
        private TextView tvTime;
        private TextView tvName;
        private TextView tvCoin;
        private ImageView ivDel;
    }
}
